﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.order
{
    public partial class GoodsOutDetail : BaseCrm
    {
        readonly t_DALWarehouseGoods theWarehouseGoods = new t_DALWarehouseGoods();
        readonly t_DALBrand theBrand = new t_DALBrand();
        readonly t_DALCompany theCompany = new t_DALCompany();
        readonly t_DALOrder theOrder = new t_DALOrder();
        readonly t_DALOrderGoods theOrderGoods = new t_DALOrderGoods(); 
        t_Brand oBrand;
        readonly t_DALGoods theGoods = new t_DALGoods();
        t_Goods oGoods;
        t_Order oOrder;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(eRule.GoodsIn);
                this.GenerateOrderCode();
                this.BindBuyCompanyList();
                this.BindCompanyList();
                this.BuildForm();

            }
        }
        void GenerateOrderCode()
        {
            this.hdOrderCode.Text = DateTime.Now.ToString("yyyyMMddHHmmssffff");
        }

        void BindCompanyList()
        {
            DataTable table = theCompany.GetList("companyTypeId in(1,2)").Tables[0];
            this.ddlCompanyList.DataValueField = "id";
            this.ddlCompanyList.DataTextField = "Name";
            this.ddlCompanyList.DataSource = table;
            this.ddlCompanyList.DataBind();
            this.ddlCompanyList.Items.Insert(0, new ExtAspNet.ListItem("选择供应商", "-1"));
            this.ddlCompanyList.SelectedValue = "-1";
        }

        void BindBuyCompanyList()
        {
            DataTable table = theCompany.GetList("companyTypeId in(0,1)").Tables[0];
            this.ddlBuyCompanyList.DataValueField = "id";
            this.ddlBuyCompanyList.DataTextField = "Name";
            this.ddlBuyCompanyList.DataSource = table;
            this.ddlBuyCompanyList.DataBind();
            this.ddlBuyCompanyList.Items.Insert(0, new ExtAspNet.ListItem("选择进货商", "-1"));
            this.ddlBuyCompanyList.SelectedValue = "-1";
        }





        void BuildForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                if (oGoods != null)
                {
                   // this.txtGoodsCode.Text = oGoods.goodsCode;
                    this.lblGoodsName.Label = "商品名称";
                    oBrand = theBrand.GetModel((int)oGoods.brandId);
                    this.lblGoodsName.Text = oBrand.brandName + "-" + oGoods.goodsName;
                }
            }
        }



        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            
  
                oOrder = new t_Order();

                oOrder.creater = int.Parse(base.LoginAdminId);
                oOrder.createTime = DateTime.Now;
                oOrder.orderCode = this.hdOrderCode.Text;
                oOrder.orderAddress = this.txtSendAddress.Text.Trim();
                oOrder.linker = this.txtLinker.Text.Trim();
                oOrder.linkerPhone = this.txtLinkerPhone.Text.Trim();
                oOrder.remark = this.txtRemark.Text.Trim();
                oOrder.OrderStates = 0;
                oOrder.orderCompanyId = int.Parse(this.ddlBuyCompanyList.SelectedItem.Value);
                oOrder.companyId = int.Parse(this.ddlCompanyList.SelectedItem.Value);

                theOrderGoods.updateOrderGoodsPrice(this.hdOrderCode.Text, (int)oOrder.orderCompanyId);

                if (theOrder.Add(oOrder) > 0)
                {
                    ExtAspNet.Alert.Show("添加成功！", "", "window.location.href='inlineOrder.aspx'");
                }
        
        }


        private void BindAddGoodsGrid()
        {
            int iCompanyId = int.Parse(this.ddlBuyCompanyList.SelectedItem.Value);
            DataTable table = theOrderGoods.GetListByOrderCode(this.hdOrderCode.Text, iCompanyId).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

        }

        protected void Window1_Close(object sender, EventArgs e)
        {
           this.BindAddGoodsGrid();
            // ExtAspNet.Alert.Show("postback回发窗口！");
        }

        protected void Window2_Close(object sender, EventArgs e)
        {
            this.BindAddGoodsGrid();
            // ExtAspNet.Alert.Show("postback回发窗口！");
        }



        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {

            if (e.CommandName == "deleted")
            {
                theOrderGoods.Delete(this.hdOrderCode.Text, int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString()));
                ExtAspNet.Alert.Show("商品删除成功！");
                this.BindAddGoodsGrid();

            }


        }

        protected void ddlBuyCompanyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindAddGoodsGrid();
        }
    }
}